package com.gxa.controller.dmt.user;

import com.gxa.common.R;
import com.gxa.common.vo.UserRequestVo;
import com.gxa.common.vo.UserVo;
import com.gxa.service.SysUserService;
import io.swagger.annotations.*;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/userManagement/user")
@Api(value = "用户管理测试",tags = "用户管理接口API")
@RequiresRoles(value = {"超级管理员"},logical = Logical.OR)
public class SysUserController {

    @Autowired
    SysUserService userService;

    /**
     * 分页显示所有用户信息
     *
     * @param currentPage
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "显示所有用户信息", notes = "显示所有用户信息",
            httpMethod = "GET", response = R.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage", value = "当前页码数默认第一页", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页显示记录数", paramType = "query", dataType = "String", required = true)
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 404, message = "找不到页面"),
            @ApiResponse(code = 500, message = "后台服务错误")
    })
    @RequestMapping(value = "/findUserList", method = RequestMethod.GET)
    @ResponseBody
    public R findUserList(@RequestParam("currentPage") Integer currentPage, @RequestParam("pageSize") Integer pageSize) {
        return userService.selectAllUser(currentPage,pageSize);
    }


    /**
     * 根据用户真实名称模糊查询用户信息列表
     *
     * @param currentPage 当前页码数默认第一页
     * @param pageSize    每页显示记录数
     * @param condition    用户真实名称
     * @return
     */
    @ApiOperation(value = "根据用户真实名称模糊查询用户信息列表", notes = "根据用户真实名称模糊查询用户信息列表",
            httpMethod = "GET", response = R.class)
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 404, message = "找不到页面"),
            @ApiResponse(code = 500, message = "后台服务错误")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage", value = "当前页码数默认第一页", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页显示记录数", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "condition", value = "条件", paramType = "query", dataType = "String", required = true)
    })
    @RequestMapping(value = "/findUserByName", method = RequestMethod.GET)
    @ResponseBody
    public R findUserByName(@RequestParam("currentPage") Integer currentPage, @RequestParam("pageSize") Integer pageSize, @RequestParam("realName") String condition) {
        return userService.selectUserByConditions(condition,currentPage,pageSize);
    }


    /**
     * 根据输入条件查询用户信息列表
     * @param userRequestVo 用户高级查询信息实体
     * @return
     */
    @ApiOperation(value = "根据输入条件查询用户信息列表", notes = "根据输入条件查询用户信息列表",
            httpMethod = "POST", response = R.class)
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 404, message = "找不到页面"),
            @ApiResponse(code = 500, message = "后台服务错误")
    })
    @RequestMapping(value = "/findUserByConditions", method = RequestMethod.POST)
    @ResponseBody
    public R findUserByConditions(@RequestBody @ApiParam(name = "userRequestVo",value = "用户高级查询信息实体",required = true) UserRequestVo userRequestVo) {
        return userService.selectUserByCondition(userRequestVo);
    }



    /**
     * 添加用户信息
     * @param userVo 用户实体（前端传一个json）
     * @return
     */
    @ApiOperation(value = "添加用户信息",notes = "添加用户信息",
            httpMethod = "POST", response = R.class)
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    @RequestMapping(value = "/addUser",method = RequestMethod.POST)
    @ResponseBody
    public R addUser(@RequestBody @ApiParam(name = "sysUser",value = "用户实体",required = true) UserVo userVo){
        return userService.addUser(userVo);
    }



    /**
     * 修改用户信息
     * @param userVo 用户实体（前端传一个json）
     * @return
     */
    @ApiOperation(value = "修改用户信息",notes = "修改用户信息",
            httpMethod = "POST", response = R.class)
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    @RequestMapping(value = "/updateUser",method = RequestMethod.POST)
    @ResponseBody
    public R updateUser(@RequestBody @ApiParam(name = "userVo",value = "用户实体",required = true) UserVo userVo){
        return userService.updateUserById(userVo);
    }


    @RequestMapping(value = "/addUserImg",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "修改用户，头像上传（使用用户id）接口",notes = "修改用户，头像上传（使用用户id）接口",httpMethod = "POST", response = R.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "img", value = "图片", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "id", value = "用户id", paramType = "query", dataType = "String", required = true)
    })
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=400,message="参数错误"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    public R addUserImg(@RequestParam("img") MultipartFile img, @RequestParam("id") Integer id, HttpServletRequest request){
        return userService.addUserImg(img,id,request);
    }


    /**
     * 用户数据回显
     * @param id 用户ID
     * @return
     */
    @ApiOperation(value = "用户数据回显",notes = "用户数据回显",
            httpMethod = "GET", response = R.class)
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "用户ID",paramType = "query",dataType = "String",required = true)
    })
    @RequestMapping(value = "/findUserById",method = RequestMethod.GET)
    @ResponseBody
    public R findUserById( @RequestParam("id")Integer id){
        return userService.selectUserById(id);
    }



    /**
     * 根据用户ID删除用户
     * @param id 用户ID
     * @return
     */
    @ApiOperation(value = "根据用户ID删除用户",notes = "根据用户ID删除用户",
            httpMethod = "POST", response = R.class)
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "用户ID",paramType = "query",dataType = "String",required = true)
    })
    @RequestMapping(value = "/delDeptById",method = RequestMethod.POST)
    @ResponseBody
    public R delDeptById( @RequestParam("id")Integer id){
        return userService.delUserById(id);
    }


    /**
     * 根据部门ID查询用户
     * @param depId 部门ID
     * @return R
     */
    @ApiOperation(value = "根据部门ID查询用户",notes = "根据部门ID查询用户",
            httpMethod = "POST", response = R.class)
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    @ApiImplicitParam(name = "depId",value = "部门ID",paramType = "query",dataType = "String",required = true)

    @RequestMapping(value = "/findUserByDepId",method = RequestMethod.POST)
    @ResponseBody
    public R findUserByDepId( @RequestParam("depId")Integer depId){
        return userService.findUserByDepId(depId);
    }



}
